package com.manage.auth;

import com.common.util.Page;
import com.model.auth.SResources;
import com.opensymphony.xwork2.ActionSupport;
import com.service.auth.IResService;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

public class ResAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(ResAction.class);
	private Integer pageNo=1;
	private Integer pageSize =20;
	
	private List<SResources> resList;
	private Integer resCount;
	private SResources res;
	private Integer id;
	private String ids;
	private String result;
	private Page page;
	
	private List<SResources> menuList;//栏目列表
	
	private List<SResources> menuTree;//栏目树结构
	
	@Resource
	private IResService resService;
	
	
	private Integer parentId = 0;
	
	
	public String resList(){
		
		try {
			menuList = resService.getResByParentId(parentId, null);//所有一级栏目的父节点ID为0
			menuTree = resService.getAllMenus();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "resList";
	}
	
	public String list() {
		try {
			if(parentId==null){
				parentId = 0;
			}
			if(parentId != 0){
				res = resService.getResById(parentId);
			}
			resCount = resService.getResConunt(parentId);
			page = new Page(resCount,pageSize,pageNo);
			resList =  resService.getResPageInfo(parentId, pageNo, pageSize);
		} catch (Exception e) {
			log.error("资源模块列表异常", e);
		}
		return "list";
	}
	
	/**
	 * 添加模块
	 * @return
	 */
	public String add(){
		
		
		return "add";
	}
	
	
	public String save(){
		try{
			if(res!=null && res.getId()!=null){
				
				resService.updateRes(res);//更新应用信息
			}else if(res!=null){
				
				res.setCreateDate(new Date());
				resService.insertRes(res);
			}
			result = "1";
		}catch(Exception e){
			result = "0";
			log.error("保存模块列表异常", e);
		}
		return null;
	}
	
	public String delete(){
		
		try{
			if(id!=null){
				resService.delResById(id);
			}
			
			if(StringUtils.isNotBlank(ids)){
				String[] idList =ids.split(",");
				for(int i=0;i<idList.length;i++){
					resService.delResById(Integer.valueOf(idList[i]));
				}
			}
			result="success";
		}catch(Exception e){
			log.error("删除模块列表异常", e);
			result="error";
		}
		return SUCCESS;
	}
	
	/**
	 * 编辑模块
	 * @return
	 */
	public String edit(){
		try{
			
			if(id!=null){
				res = resService.getResById(id);
			}
		}catch(Exception e){
			log.error("查询模块详细信息", e);
		}
		
		return SUCCESS;
	}
	
	/**
	 * 更新模块详细信息
	 * @return
	 */
	public String update(){
		try{
			if(res!=null){
				resService.updateRes(res);
			}
			result = "1";
		}catch(Exception e){
			result = "0";
			log.error("更新模块详细信息", e);
		}
		return "edit";
	}
	
	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<SResources> getMenuTree() {
		return menuTree;
	}

	public void setMenuTree(List<SResources> menuTree) {
		this.menuTree = menuTree;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getResCount() {
		return resCount;
	}


	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public void setResCount(Integer resCount) {
		this.resCount = resCount;
	}



	public Integer getParentId() {
		return parentId;
	}

	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}

	public List<SResources> getResList() {
		return resList;
	}

	public void setResList(List<SResources> resList) {
		this.resList = resList;
	}

	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public List<SResources> getMenuList() {
		return menuList;
	}

	public void setMenuList(List<SResources> menuList) {
		this.menuList = menuList;
	}

	public SResources getRes() {
		return res;
	}

	public void setRes(SResources res) {
		this.res = res;
	}

}
